package com.explorati.design_pattern.structural.proxy.example2.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Date;

public class GamePlayerDynamicProxy implements InvocationHandler {
	private Object target;

	public GamePlayerDynamicProxy(Object target) {
		this.target = target;
	}

	public Object bind() {
		Class cls = target.getClass();
		return Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), this);
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		if ("killBoss".equals(method.getName())) {
			this.log();
			Object object = method.invoke(target, args);
			return object;
		}
		if ("upGrade".equals(method.getName())) {
			this.count();
			Object object = method.invoke(target, args);
			return object;
		} else {
			return null;
		}
	}

	private void log() {
		System.out.println("打怪时间" + new Date().toString());
	}

	private void count() {
		System.out.println("升1级耗费50小时！");
	}

}
